package com.yunxiao.cloud.elasticsearch.service.impl;

import com.yunxiao.cloud.common.util.DateUtil;
import com.yunxiao.cloud.elasticsearch.entity.RequestLog;
import com.yunxiao.cloud.elasticsearch.respository.RequestLogRepository;
import com.yunxiao.cloud.elasticsearch.service.RequestLogService;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @description: RequestLogServiceImpl
 * @author: yunxiao
 * @date: 2020/1/14 9:52
 * @version: V1.0
 **/
@Service
public class RequestLogServiceImpl implements RequestLogService {

    @Resource
    private RequestLogRepository requestLogRepository;

    @Override
    public String esInsert(Integer num) {
        for (int i = 0; i < num; i++) {
            RequestLog requestLog = new RequestLog();
            requestLog.setId(System.currentTimeMillis());
            requestLog.setOrderNo(DateUtil.format(new Date(), DateUtil.DATE_FORMAT_PATTERN) + System.currentTimeMillis());
            requestLog.setUserId("userId" + i);
            requestLog.setUserName("张三" + i);
            requestLog.setCreateTime(DateUtil.format(new Date(), DateUtil.DATE_TIME_FORMAT_PATTERN));
            requestLogRepository.save(requestLog);
        }
        return "success";
    }

    @Override
    public Iterable<RequestLog> esFindAll() {
        return requestLogRepository.findAll();
    }

    @Override
    public String esUpdateById(RequestLog requestLog) {
        requestLogRepository.save(requestLog);
        return "success";
    }

    @Override
    public Optional<RequestLog> esSelectById(Long id) {
        return requestLogRepository.findById(id);
    }

    @Override
    public Iterable<RequestLog> esFindOrder() {
        // 创建时间正序
        List<Sort.Order> orderList = new ArrayList<>();
        Sort.Order sort1 = new Sort.Order(Sort.Direction.ASC, "createTime.keyword1");
        orderList.add(sort1);
        Sort orders = Sort.by(orderList);
        return requestLogRepository.findAll(orders);
    }

    @Override
    public Iterable<RequestLog> esFindOrders() {
        List<Sort.Order> orderList = new ArrayList<>();
        Sort.Order sort1 = new Sort.Order(Sort.Direction.ASC, "createTime.keyword2");
        Sort.Order sort2 = new Sort.Order(Sort.Direction.DESC, "userName.keyword");
        orderList.add(sort1);
        orderList.add(sort2);
        Sort orders = Sort.by(orderList);
        return requestLogRepository.findAll(orders);
    }

    @Override
    public Iterable<RequestLog> search() {
        // 全文搜索关键字
        /*
         * 多条件查询
         */
        QueryBuilder builder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("userName", "张三"))
                .must(QueryBuilders.matchQuery("orderNo", "20190613736278243"));
        return requestLogRepository.search(builder);
    }
}
